<?php
/**
 * SQLサーババイトチェック
 *
 * @author tanaka
 * @package Validate
 * @subpackage ByteSqlServer
 */
class Validate_ByteSqlServer extends Zend_Validate_StringLength
{
	const SQL_SRV_INTERNAL_ENCODING = 'sjis-win';
	
	/**
	 * Defined by Zend_Validate_Interface
	 *
	 * Returns true if and only if the string length of $value is at least the min option and
	 * no greater than the max option (when the max option is not null).
	 *
	 * @param  string $value
	 * @return boolean
	 */
	public function isValid($value)
	{
		if (!is_string($value)) {
			$this->_error(self::INVALID);
			return false;
		}

		$this->_setValue($value);
		
		$binary = "";
		
		if ($this->_encoding !== null) {
			$binary = mb_convert_encoding($value, self::SQL_SRV_INTERNAL_ENCODING, $this->_encoding);
		}
		else {
			$binary = mb_convert_encoding($value, self::SQL_SRV_INTERNAL_ENCODING);
		}
		
		$length = strlen($binary);

		if ($length < $this->_min) {
			$this->_error(parent::TOO_SHORT);
		}

		if (null !== $this->_max && $this->_max < $length) {
			$this->_error(parent::TOO_LONG);
		}

		if (count($this->_messages)) {
			return false;
		}
		else {
			return true;
		}
	}
}
